/***********************************************
 This file is part of redia-bapps-main-ios.
 
 Copyright (c) 2015 Redia A/S
 
 Redia-bapps-main-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-bapps-main-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-bapps-main-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */

//
//  BAAddReservationViewController.m
//  redia-bapps-ios
//
//  Created by Henrik Lynbech on 29/08/13.
//  Copyright (c) 2013 redia. All rights reserved.
//

#import "BAAddReservationViewController.h"
#import "BALoginViewController.h"

@interface BAAddReservationViewController () {
    UIAlertView* confirmMakeReservationAlert;
    UIAlertView* reservationCompletionAlert;
    NSString* _reservationId;
}

@end

@implementation BAAddReservationViewController

+(void)createAddReservationDialogWithReservationId:(NSString*)reservationId title:(NSString*)title
{
    BAAddReservationViewController* new_dialog_viewcontroller = [[self class] createBlackCoverDialog];

    [new_dialog_viewcontroller performAddReservation:reservationId withTitle:title];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (alertView==confirmMakeReservationAlert) {
        if (buttonIndex==1) {
            //user clicked OK
            [self.loadingIndicator startAnimating];
            [BALoginViewController checkLoginStatusFromViewController:self
                                                          resultBlock:^{
                                                              [BARPCManager addReservationAsyncFromView:self
                                                                                          reservationId:_reservationId
                                                                                            resultBlock:^(id result) {
                                                                                                [self.loadingIndicator stopAnimating];
                                                                                                
                                                                                                reservationCompletionAlert = [[UIAlertView alloc] initWithTitle:@"Reservering" message:@"Reserveringen blev gennemført." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                                                                                                [[NSNotificationCenter defaultCenter]
                                                                                                 postNotificationName:BA_RESERVATION_NEW_RESERVATION_ADDED
                                                                                                 object:self];
                                                                                                [reservationCompletionAlert show];
                                                                                            }
                                                                                             errorBlock:^(int errorCode, NSString *errorMessage) {
                                                                                                 [self.loadingIndicator stopAnimating];
                                                                                                 reservationCompletionAlert = [[UIAlertView alloc] initWithTitle:@"Reserveringen kunne ikke gennemføres" message:errorMessage delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                                                                                                 [reservationCompletionAlert show];
                                                                                                 
                                                                                             }];
                                                          }
                                                           errorBlock:^(int errorCode, NSString *errorMessage) {
                                                               UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Kunne ikke gennemføre din reservering"
                                                                                                               message:errorMessage
                                                                                                              delegate:nil
                                                                                                     cancelButtonTitle:@"OK"
                                                                                                     otherButtonTitles:nil];
                                                               [alert show];
                                                               [self dismissBlackCover];
                                                           }];
        } else {
            //user clicked Cancel
            [self dismissBlackCover];
        }
    } else if (alertView == reservationCompletionAlert) {
        [self dismissBlackCover];
    }
}

-(void)performAddReservation:(NSString *)reservationId withTitle:(NSString*)title
{
    _reservationId = reservationId;
    
    confirmMakeReservationAlert = [[UIAlertView alloc] initWithTitle:@"Reservering" message:[NSString stringWithFormat:@"Vil du reservere %@?",title] delegate:self cancelButtonTitle:@"Annuller" otherButtonTitles:@"OK", nil];
    //FIXME: show name of preferred library in alert
    
    [confirmMakeReservationAlert show];
}


@end
